﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Collections;

namespace ReflectorHelper
{
    public partial class frmMain : Form
    {
        #region 变量
        ArrayList alAllFiles = new ArrayList();
        string SavePath = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\ReflectorHelper";
        #endregion

        public frmMain()
        {
            InitializeComponent();
        }

        #region 界面操作
        private void tsmiOpenFiles_Click(object sender, EventArgs e)
        {
            this.OpenFiles();
        }

        private void tsmiOpenFolder_Click(object sender, EventArgs e)
        {
            
        }

        private void tsmiSave_Click(object sender, EventArgs e)
        {
            this.SaveFiles();
        }

        private void tsmiEixt_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void tsBtnConvert_Click(object sender, EventArgs e)
        {
            this.Convert();
        }

        private void tsBtnOpenFiles_Click(object sender, EventArgs e)
        {
            this.OpenFiles();
        }

        private void tsBtnOpenFolder_Click(object sender, EventArgs e)
        {

        }

        private void tsBtnSave_Click(object sender, EventArgs e)
        {
            this.SaveFiles();
        }
        #endregion

        #region Private Method
        private void SaveFiles()
        {
            if (DialogResult.OK == this.folderBrowserDialog1.ShowDialog())
            {
                SavePath = this.folderBrowserDialog1.SelectedPath;
            }
        }

        private void OpenFiles()
        {
            if (DialogResult.OK == this.ofdOpenFile.ShowDialog())
            {
                string[] st = this.ofdOpenFile.FileNames;

                foreach (string s in st)
                {
                    this.lbFiles.Items.Add(s);
                    alAllFiles.Add(s);
                }
            }
        }

        private void Convert()
        {
            try
            {
                if (this.lbFiles.Items.Count == 0 || string.IsNullOrEmpty(SavePath))
                {
                    MessageBox.Show("请选择路径！");
                    return;
                }
                foreach (object ss in alAllFiles)
                {
                    string s = (string)ss;
                    ArrayList al = FileHelper.ReadTxtFile(s, "\n");
                    FileInfo fi = new FileInfo(s);
                    if (!string.IsNullOrEmpty(SavePath))
                    {
                        string FilePath = SavePath + "\\" + fi.Directory.Name;
                        if (!Directory.Exists(FilePath))
                        {
                            Directory.CreateDirectory(FilePath);
                        }

                        FileHelper.WriteTxtFile(FilePath + "\\" + fi.Name, "\n", al);

                        this.lbFiles.Items.Remove(s);
                    }
                }

                MessageBox.Show("Completed!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion
    }
}
